package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import adapter.MyadpterAdd;
import bean.Adress;

public class SiteActivity extends AppCompatActivity {

    /**
     * 返回
     */
    private TextView mImageBack;
    /**
     * 管理收货地址
     */
    private TextView mTvTitle;
    private RelativeLayout mFrl;
    private RecyclerView mRecyceler;
    /**
     * 新建地址
     */
    private TextView mTvAddAdd;
    private ArrayList<Adress> mStrings;
    private MyadpterAdd myadpterAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site);
        initView();
        initciews();
    }

    private void initciews() {
         myadpterAdd.setOnCreateLayout(new MyadpterAdd.OnCreateLayout() {
             @Override
             public void OnCratek1(int position) {
                 Intent intent = new Intent(SiteActivity.this, ReceivingActivity.class);
                 startActivityForResult(intent, 1);  //请求码
             }

             @Override
             public void OnCratek2(int position) {
                 mStrings.remove(position);
                 myadpterAdd.notifyDataSetChanged();
             }

             @Override
             public void OnCratek3(int position) {
                 Adress adress = mStrings.get(position);
                 //  Toast.makeText(SiteActivity.this, "监听 ", Toast.LENGTH_SHORT).show();
                 Intent intent = new Intent(SiteActivity.this, TiJiaoActivity.class);
                 EventBus.getDefault().post(adress);
                 startActivity(intent);
             }
         });
        mTvAddAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //新建地址信息
                Intent intent = new Intent(SiteActivity.this, ReceivingActivity.class);
                startActivityForResult(intent, 1);  //请求码

            }
        });
        //返回

        mImageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView() {
        mImageBack = (TextView) findViewById(R.id.image_back);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mFrl = (RelativeLayout) findViewById(R.id.frl);
        mRecyceler = (RecyclerView) findViewById(R.id.recyceler);
        mRecyceler.setLayoutManager(new LinearLayoutManager(this));
        mTvAddAdd = (TextView) findViewById(R.id.tv_addAdd);
        mStrings = new ArrayList<>();
        myadpterAdd = new MyadpterAdd(mStrings, this);
        mRecyceler.setAdapter(myadpterAdd);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 2) {
            String name = data.getStringExtra("name");
            String phone = data.getStringExtra("phone");
            String address = data.getStringExtra("address");
            String detailed = data.getStringExtra("detailed");
            Adress adress = new Adress(name, phone, address, detailed);
            Log.i("TAG", "onActivityResult: "+adress.toString());
            mStrings.add(adress);
            myadpterAdd.notifyDataSetChanged();
        }
    }
}
